package com.chixing.controller;

import com.chixing.pojo.Customer;
import com.chixing.pojo.MyOrder;
import com.chixing.service.*;
import com.chixing.util.DateUtil;
import com.chixing.util.PrimaryKeyUtil;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.UUID;

/**
 * @author Wesley
 * @version 1.0
 * @date 2021/10/5 13:47
 */
@Controller
public class MyOrderController {
    @Autowired
    private MyOrderService myOrderService;

    @Autowired
    private CarService carService;

    @Autowired
    private MyCouponService myCouponService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CouponService couponService;

    @Autowired PrimaryKeyUtil primaryKeyUtil;

    // 第三步页面绑定
    @GetMapping("myOrder_third/{myOrderId}")
    public ModelAndView getById(@PathVariable("myOrderId") Integer myOrderId){
        ServerResponse serverResponse = myOrderService.getById(myOrderId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse",serverResponse);
        mav.setViewName("order/third_step_getter_information");
        return mav;
    }

    //第四步页面绑定
    @GetMapping("myOrder_forth/{myOrderId}")
    public ModelAndView getByOrderId(@PathVariable("myOrderId") Integer myOrderId){
        ServerResponse serverResponse = myOrderService.getById(myOrderId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse",serverResponse);
        mav.setViewName("order/forth_step_commit");
        return mav;
    }

    //订单详情绑定
    @GetMapping("order_detail/{myOrderId}")
    public ModelAndView getByMyOrderId(@PathVariable("myOrderId") Integer myOrderId){
        ServerResponse serverResponse = myOrderService.getByOrderId(myOrderId);
        Integer couponId = myOrderService.getCouponId(myOrderId);
        Float couponPrice = couponService.getAmountById(couponId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse",serverResponse);
        mav.addObject("couponPrice",couponPrice);
        mav.setViewName("order/order_detail");
        return mav;
    }

    @GetMapping("to_my_order")
    public ModelAndView toMyOrder(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("order/my_order");
        return modelAndView;
    }

    //我的订单数据绑定
    @GetMapping("my_order")
    @ResponseBody
    public ServerResponse getCustomerOrder(Integer custId,Integer status,Integer pageNum){
        if (pageNum == null)
            pageNum=1;
        return myOrderService.getCustomerOrder(custId,status,pageNum);
    }

    //创建订单
    @PostMapping("createOrder")
    public ModelAndView saveOrder(Integer custId,
                                  Integer carId,
                                  Integer borrowStoreId,
                                  Integer couponId,
                                  Integer returnStoreId,
                                  String getCarTime,
                                  String returnCarTime,
                                  Integer myOrderLeaseDays,
                                  Float myOrderPrice){

        ModelAndView mav = new ModelAndView();

        mav.addObject("custId",custId);
        mav.addObject("carId",carId);
        mav.addObject("borrowStoreId",borrowStoreId);
        mav.addObject("couponId",couponId);
        mav.addObject("returnStoreId",returnStoreId);
        mav.addObject("getCarTime",getCarTime);
        mav.addObject("returnCarTime",returnCarTime);
        mav.addObject("myOrderLeaseDays",myOrderLeaseDays);
        mav.addObject("myOrderPrice",myOrderPrice);

        mav.setViewName("order/fileUpload");
        return mav;
    }

    //确认订单页面
    @GetMapping("myOrder_second")
    public ModelAndView getInstance(Integer custId,
                                    Integer carId,
                                    Integer borrowStoreId,
                                    Integer returnStoreId,
                                    String getCarTime,
                                    String returnCarTime){

        ServerResponse customerCoupon = myCouponService.getCustomerCoupon1(custId);
        ServerResponse carDetail = carService.getCarDetail(carId);
        ServerResponse borrowStore = storeService.getById(borrowStoreId);
        ServerResponse returnStore = storeService.getById(returnStoreId);
        Date getTime = DateUtil.stringToDate(getCarTime, "yyyy-MM-dd HH:mm");
        Date returnTime = DateUtil.stringToDate(returnCarTime, "yyyy-MM-dd HH:mm");

        ModelAndView mav = new ModelAndView();
        mav.addObject("custId",custId);
        mav.addObject("carId",carId);
        mav.addObject("borrowStoreId",borrowStoreId);
        mav.addObject("returnStoreId",returnStoreId);
        mav.addObject("getCarTime",getCarTime);
        mav.addObject("returnCarTime",returnCarTime);
        mav.addObject("customerCoupon",customerCoupon);
        mav.addObject("carDetail",carDetail);
        mav.addObject("borrowStore",borrowStore);
        mav.addObject("returnStore",returnStore);
        mav.addObject("getCarTime",getCarTime);
        mav.addObject("returnCarTime",returnCarTime);
        mav.addObject("days",(returnTime.getTime()-getTime.getTime())/(1000*60*60*24));
        mav.setViewName("order/second_step");
        return mav;
    }


    //订单第二步页面绑定
    @GetMapping("getMyOrderByOrderId/{myOrderId}")
    @ResponseBody
    public ModelAndView getMyOrderStoreCar(@PathVariable("myOrderId") Integer orderId){
        ServerResponse serverResponse=myOrderService.getById(orderId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("order",(MyOrder)serverResponse.getData());
        mav.setViewName("order/third_step_getter_information");
        return mav;
    }

    //用户信息填写保存订单
    @PostMapping("saveMyOrder")
    public String createOrder(Integer custId,
                                    Integer carId,
                                    Integer borrowStoreId,
                                    Integer couponId,
                                    Integer returnStoreId,
                                    String getCarTime,
                                    String returnCarTime,
                                    String getterName,
                                    String getterIdCard,
                                    String getterDriverStartTime,
                                    String getterDriverEndTime,
                                    String getterDriverType,
                                    Integer myOrderLeaseDays,
                                    Float myOrderPrice,
                                    String custGender){

        //利用UUID生成订单号
        String[] chars = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-","");
        for (int j=0;j<12;j++) {
            String str=uuid.substring(j*2,j*2+2);
            int par=Integer.parseInt(str,24);
            shortBuffer.append(chars[par % 10]);
        }
        Long orderNum = Long.valueOf(shortBuffer.toString());

        //利用UUID生成车牌号
        String[] chars1 = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9","A","B","C","D","E","F","G",
                "H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        StringBuffer shortBuffer1 = new StringBuffer();
        String uuid1 = UUID.randomUUID().toString().replace("-","");
        for (int j=0;j<6;j++) {
            String str=uuid1.substring(j*4,j*4+4);
            int par=Integer.parseInt(str,36);
            shortBuffer1.append(chars1[par % 36]);
        }
        String carNum = "苏E" + shortBuffer1;

        Integer orderId = primaryKeyUtil.getOrderId();

        MyOrder myOrder = new MyOrder();

        myOrder.setMyOrderId(orderId);
        myOrder.setCarId(carId);
        myOrder.setCustId(custId);
        myOrder.setStoreBorrowId(borrowStoreId);
        myOrder.setMyOrderNumber(orderNum);
        myOrder.setCouponId(couponId);
        myOrder.setStoreReturnId(returnStoreId);
        myOrder.setGetCarTime(DateUtil.stringToDate(getCarTime,"yyyy-MM-dd HH:mm"));
        myOrder.setReturnCarTime(DateUtil.stringToDate(returnCarTime,"yyyy-MM-dd HH:mm"));
        myOrder.setGetterName(getterName);
        myOrder.setGetterIdCard(getterIdCard);
        myOrder.setGetterDriverLicenseStartTime(DateUtil.stringToDate(getterDriverStartTime,"yyyy-MM-dd"));
        myOrder.setGetterDriverLicenseEndTime(DateUtil.stringToDate(getterDriverEndTime,"yyyy-MM-dd"));
        myOrder.setGetterDriverLicenseType(getterDriverType);
        myOrder.setMyOrderTime(new Date());
        myOrder.setMyOrderLeaseDays(myOrderLeaseDays);
        myOrder.setCarNumber(carNum);
        myOrder.setMyOrderPrice(myOrderPrice);
        //订单状态：3为待支付
        myOrder.setStatus(3);
        myOrderService.saveMyOrder(myOrder);
        customerService.updateCustomer(custId,getterName,custGender,getterIdCard,DateUtil.stringToDate(getterDriverStartTime,"yyyy-MM-dd"),DateUtil.stringToDate(getterDriverEndTime,"yyyy-MM-dd"),getterDriverType);

        MyOrder order = (MyOrder)myOrderService.getById(orderId).getData();

        ModelAndView mav = new ModelAndView();
        mav.addObject("order",order);
        mav.setViewName("order/third_step_getter_information");
        return "redirect:/getMyOrderByOrderId/" + orderId;
    }

    //修改用户信息
    @PutMapping("updateCustomer")
    public ModelAndView updateCustomer(Long custTel,String customerName,String custGender,String custIdCard,
                                       Date driverLicenseStartTime,Date driverLicenseEndTime,
                                       String driverLicenseType){
        Customer customer=new Customer();
        customer.setCustTel(custTel);
        customer.setCustName(customerName);
        customer.setCustGender(custGender);
        customer.setCustIdCard(custIdCard);
        customer.setCustDriverStartTime(driverLicenseStartTime);
        customer.setCustDriverEndTime(driverLicenseEndTime);
        customer.setCustDriverType(driverLicenseType);
        ModelAndView mav=new ModelAndView();
        ServerResponse serverResponse=myOrderService.updateCustomer(customer);
        mav.addObject("customerSave",serverResponse);
        mav.setViewName("order/fileUpload");
        return mav;
    }

    //修改订单状态为4（已取消）
    @PutMapping("updateOrderStatus4")
    @ResponseBody
    public ServerResponse updateOrder(Integer myOrderId,
                                      Integer custId,
                                      Integer carId,
                                      Integer storeBorrowId,
                                      Integer couponId,
                                      Long myOrderNumber,
                                      Integer storeReturnId,
                                      Date getCarTime,
                                      Date returnCarTime,
                                      String getterName,
                                      String getterIdCard,
                                      Date getterDriverLicenseStartTime,
                                      Date getterDriverLicenseEndTime,
                                      Date myOrderTime,
                                      String getterDriverLicenseType,
                                      Integer myOrderLeaseDays,
                                      String carNumber,
                                      Float myOrderPrice,
                                      Integer status,
                                      String other1,
                                      String other2){
        MyOrder myOrder = new MyOrder();
        myOrder.setMyOrderId(myOrderId);
        myOrder.setCustId(custId);
        myOrder.setCarId(carId);
        myOrder.setStoreBorrowId(storeBorrowId);
        myOrder.setStoreReturnId(storeReturnId);
        myOrder.setCouponId(couponId);
        myOrder.setGetCarTime(getCarTime);
        myOrder.setMyOrderNumber(myOrderNumber);
        myOrder.setReturnCarTime(returnCarTime);
        myOrder.setGetterName(getterName);
        myOrder.setGetterIdCard(getterIdCard);
        myOrder.setGetterDriverLicenseStartTime(getterDriverLicenseStartTime);
        myOrder.setGetterDriverLicenseEndTime(getterDriverLicenseEndTime);
        myOrder.setMyOrderTime(myOrderTime);
        myOrder.setGetterDriverLicenseType(getterDriverLicenseType);
        myOrder.setMyOrderLeaseDays(myOrderLeaseDays);
        myOrder.setCarNumber(carNumber);
        myOrder.setMyOrderPrice(myOrderPrice);
        myOrder.setStatus(status);
        myOrder.setOther1(other1);
        myOrder.setOther2(other2);
        ServerResponse serverResponse = myOrderService.updateOrderStatus4(myOrder);
        return serverResponse;
    }

    @GetMapping("getMyOrder/{myOrderId}")
    @ResponseBody
    public ServerResponse getOrderById(@PathVariable("myOrderId") Integer myOrderId){
        ServerResponse serverResponse = myOrderService.getByOrderId(myOrderId);
        return serverResponse;
    }
}
